/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.action;

import static javacommon.util.extjs.Struts2JsonHelper.outJson;
import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.CtgData;
import javacommon.util.Notes;
import javacommon.util.ResInfo;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.core.model.Module;
import com.tech15.core.service.ModuleManager;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class ModuleAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String LIST_JSP= "/sys/modulePermission_list.jsp";
	protected static final String MANAGE = "/sys/module_manage.jsp";
	protected static final String MODULE_TREE = "/sys/module_tree.jsp";
	protected static final String MODULE_LIST = "/sys/module_list.jsp";
	protected static final String CREATE_JSP = "/sys/module_add.jsp";
	protected static final String EDIT_JSP = "/sys/module_edit.jsp";
	
	private ModuleManager moduleManager;
	
	private Module module;
	Long id = null;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			module = new Module();
		} else {
			module = (Module)moduleManager.getById(id);
		}
	}
	
	public void setModuleManager(ModuleManager manager) {
		this.moduleManager = manager;
	}	
	
	public Object getModel() {
		return module;
	}
	
	public void setMdid(Long val) {
		this.id = val;
	}
	
	public void getModuleCount(){
		Integer count = moduleManager.getModuleCount(this.getLoginInfo().getUserid(), Notes.APPID);
		getRequest().setAttribute("mdcount",count);
	}

	/** 执行搜索 */
	public String list() {
		List<CtgData> list = moduleManager.findModuleTree(Notes.APPID);
		getRequest().setAttribute("list", list);
		return LIST_JSP;
	}
	
	public String manage(){
		return MANAGE;
	}
	
	public void loadTreeData(){
		List<CtgData> item = moduleManager.loadModuleTree(appid);
		List<CtgData> newitem = new ArrayList<CtgData>();
		CtgData ctg = new CtgData();
		ctg.setId("-1");
		ctg.setText("全部模块");
		ctg.setItem(item);
		newitem.add(ctg);
		
		Map map = new HashMap();
		map.put("id",0);
		map.put("item",newitem);
		outJson(map);
	}
	
	public String tree(){
		return MODULE_TREE;
	}
	
	public String listByPid(){
		List<Module> list = moduleManager.listByPid(id);
		getRequest().setAttribute("list", list);
		return MODULE_LIST;
	}
	
	public String create() {
		return CREATE_JSP;
	}
	
	public String edit() {
		return EDIT_JSP;
	}
	
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			moduleManager.save(module);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			moduleManager.update(module);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	public void saveSubs() {
		Map<String, Object> resInfo = null;
		try{
			moduleManager.saveSubs(selectCheck,mdpid,module.getAppid());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	public void delete() {
		Map<String, Object> resInfo = null;
		try{
			moduleManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	public void bathDelete() {
		Map<String, Object> resInfo = null;
		try{
			for(Long typeId : selectCheck){
				moduleManager.removeById(typeId);
			}
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	private Integer appid;
	private Long[] selectCheck;
	private Long mdpid;

	public Long getMdpid() {
		return mdpid;
	}

	public void setMdpid(Long mdpid) {
		this.mdpid = mdpid;
	}

	public Long[] getSelectCheck() {
		return selectCheck;
	}

	public void setSelectCheck(Long[] selectCheck) {
		this.selectCheck = selectCheck;
	}

	public Integer getAppid() {
		return appid;
	}

	public void setAppid(Integer appid) {
		this.appid = appid;
	}
}
